/*******************************************************************************
* Project: QSAR(Cute Software Architecture) 
*
* Copyright: Copyright(C) 2024 by YuetingBen, All Rights Reserved
*
* File name: PduR.c
*
* Brief: ;
*
* Author: Yueting.Ben
*
* Module: PDUR
*
* Revision:
* --------------------------------------------------------------------------------
* v0.1: 
* --Yueting.Ben, 2024-02-26 
* --Initial release

**********************************************************************************/

/** HEADER FILES **/
#include "PduR.h"


/** MACRO DEFINITION **/


/** TYPEDEF DEFINITION **/


/** GLOBAL DATA DEFINITION **/


/** LOCAL DATA DEFINITION **/


/** LOCAL FUNCTION DECLARATION **/


/** GLOBAL FUNCTION DECLARATION **/
Std_ReturnType PduR_LoCancelTransmit(
    PduIdType TxPduId 
);

Std_ReturnType PduR_LoTransmit(
    PduIdType TxPduId, 
    const PduInfoType* PduInfoPtr
);

Std_ReturnType PduR_LoTpCancelReceive(
    PduIdType RxPduId
);

Std_ReturnType PduR_LoTpCancelTransmit(
    PduIdType TxPduId 
);

Std_ReturnType PduR_LoTpTransmit(
    PduIdType TxPduId, 
    PduInfoType* PduInfoPtr
);

void PduR_UpRxIndication(
    PduIdType RxPduId, 
    PduInfoType* PduInfoPtr
);

Std_ReturnType PduR_UpTriggerTransmit(
    PduIdType TxPduId, 
    PduInfoType* PduInfoPtr
);

void PduR_UpTxConfirmation(
    PduIdType TxPduId, 
    Std_ReturnType result
);

BufReq_ReturnType PduR_UpTpCopyRxData(
    PduIdType id,
    const PduInfoType* info,
    PduLengthType* bufferSizePtr
);

BufReq_ReturnType PduR_UpTpCopyTxData(
    PduIdType id,
    const PduInfoType* info,
    const RetryInfoType* retry,
    PduLengthType* availableDataPtr
);

BufReq_ReturnType PduR_UpTpStartOfReception(
    PduIdType id,
    const PduInfoType* info,
    PduLengthType TpSduLength,
    PduLengthType* bufferSizePtr
);

void PduR_UpTpTpRxIndication(
    PduIdType id,
    Std_ReturnType result
);

void PduR_UpTpTpTxConfirmation(
    PduIdType id,
    Std_ReturnType result
);


Std_ReturnType PduR_Routing(
    PduIdType id,
    const PduInfoType* PduInfoPtr
);
/** LOCAL FUNCTION **/


/** GLOBAL FUNCTION **/


/*******************************************************************************
* -Name            PduR_LoCancelTransmit
* -Brief           None
* -Details         None
* -Param[in]       TxPduId: Source id for routing    
* -Return          E_OK: Request accomplished successful
*                  E_NOT_OK:
*******************************************************************************/
Std_ReturnType PduR_LoCancelTransmit(
    PduIdType TxPduId 
)
{
    Std_ReturnType retVal = E_OK;

    uint8 routingIndex;
    PduR_RoutingPathType* routingPathPtr;
    PduR_ProviderFctIfType* providerFctIfPtr;

    for(routingIndex = 0u; routingIndex < PDURCONF_ROUTINGPATH_MAX; routingIndex++)
    {
        routingPathPtr = (PduR_RoutingPathType*)&PduR_RoutingPath[routingIndex];
        if(TxPduId == routingPathPtr->srcPduPtr->sourcePduHandleId)
        {
            providerFctIfPtr = (PduR_ProviderFctIfType*)&PduR_ProviderFctIf[routingPathPtr->routingMode];
            if(NULL != providerFctIfPtr->loCancelTransmitFct)
            {
                retVal = providerFctIfPtr->loCancelTransmitFct(routingPathPtr->destPduPtr->destPduHandleId);
            }
        }
    }

    return(retVal);
}


/*******************************************************************************
* -Name            PduR_LoTransmit
* -Brief           None
* -Details         None
* -Param[in]       TxPduId: Source id for routing    
* -Param[in]       PduInfoPtr: PduInfo
* -Return          E_OK: Request accomplished successful
*                  E_NOT_OK:
*******************************************************************************/
Std_ReturnType PduR_LoTransmit(
    PduIdType TxPduId, 
    const PduInfoType* PduInfoPtr
)
{
    Std_ReturnType retVal = E_OK;

    uint8 routingIndex;
    PduR_RoutingPathType* routingPathPtr;
    PduR_ProviderFctIfType* providerFctIfPtr;

    for(routingIndex = 0u; routingIndex < PDURCONF_ROUTINGPATH_MAX; routingIndex++)
    {
        routingPathPtr = (PduR_RoutingPathType*)&PduR_RoutingPath[routingIndex];
        if(TxPduId == routingPathPtr->srcPduPtr->sourcePduHandleId)
        {
            providerFctIfPtr = (PduR_ProviderFctIfType*)&PduR_ProviderFctIf[routingPathPtr->routingMode];
            if(NULL != providerFctIfPtr->loTransmitFct)
            {
                retVal = providerFctIfPtr->loTransmitFct(routingPathPtr->destPduPtr->destPduHandleId, PduInfoPtr);
            }
        }
    }

    return(retVal);
}


/*******************************************************************************
* -Name            PduR_LoTpCancelReceive
* -Brief           None
* -Details         None
* -Param[in]       RxPduId: Source id for routing            
* -Return          E_OK: Request accomplished successful
*                  E_NOT_OK:
*******************************************************************************/
Std_ReturnType PduR_LoTpCancelReceive(
    PduIdType RxPduId
)
{
    Std_ReturnType retVal = E_OK;

    uint8 routingIndex;
    PduR_RoutingPathType* routingPathPtr;
    PduR_ProviderFctIfType* providerFctIfPtr;

    for(routingIndex = 0u; routingIndex < PDURCONF_ROUTINGPATH_MAX; routingIndex++)
    {
        routingPathPtr = (PduR_RoutingPathType*)&PduR_RoutingPath[routingIndex];
        if(RxPduId == routingPathPtr->srcPduPtr->sourcePduHandleId)
        {
            providerFctIfPtr = (PduR_ProviderFctIfType*)&PduR_ProviderFctIf[routingPathPtr->routingMode];
            if(NULL != providerFctIfPtr->loTpCancelReceiveFct)
            {
                retVal = providerFctIfPtr->loTpCancelReceiveFct(routingPathPtr->destPduPtr->destPduHandleId);
            }
        }
    }

    return(retVal);
}


/*******************************************************************************
* -Name            PduR_LoTpCancelTransmit
* -Brief           None
* -Details         None
* -Param[in]       TxPduId: Source id for routing            
* -Return          E_OK: Request accomplished successful
*                  E_NOT_OK:
*******************************************************************************/
Std_ReturnType PduR_LoTpCancelTransmit(
    PduIdType TxPduId 
)
{
    Std_ReturnType retVal = E_OK;

    uint8 routingIndex;
    PduR_RoutingPathType* routingPathPtr;
    PduR_ProviderFctIfType* providerFctIfPtr;

    for(routingIndex = 0u; routingIndex < PDURCONF_ROUTINGPATH_MAX; routingIndex++)
    {
        routingPathPtr = (PduR_RoutingPathType*)&PduR_RoutingPath[routingIndex];
        if(TxPduId == routingPathPtr->srcPduPtr->sourcePduHandleId)
        {
            providerFctIfPtr = (PduR_ProviderFctIfType*)&PduR_ProviderFctIf[routingPathPtr->routingMode];
            if(NULL != providerFctIfPtr->loTpCancelTransmitFct)
            {
                retVal = providerFctIfPtr->loTpCancelTransmitFct(routingPathPtr->destPduPtr->destPduHandleId);
            }
        }
    }

    return(retVal);
}


/*******************************************************************************
* -Name            PduR_LoTpTransmit
* -Brief           None
* -Details         None
* -Param[in]       TxPduId: Source id for routing        
* -Param[in]       PduInfoPtr: PduInfo
* -Param[in]       bufferSizePtr:       
* -Return          E_OK: Request accomplished successful
*                  E_NOT_OK:
*******************************************************************************/
Std_ReturnType PduR_LoTpTransmit(
    PduIdType TxPduId, 
    PduInfoType* PduInfoPtr
)
{
    Std_ReturnType retVal = E_OK;

    uint8 routingIndex;
    PduR_RoutingPathType* routingPathPtr;
    PduR_ProviderFctIfType* providerFctIfPtr;

    for(routingIndex = 0u; routingIndex < PDURCONF_ROUTINGPATH_MAX; routingIndex++)
    {
        routingPathPtr = (PduR_RoutingPathType*)&PduR_RoutingPath[routingIndex];
        if(TxPduId == routingPathPtr->srcPduPtr->sourcePduHandleId)
        {
            providerFctIfPtr = (PduR_ProviderFctIfType*)&PduR_ProviderFctIf[routingPathPtr->routingMode];
            if(NULL != providerFctIfPtr->loTpTransmitFct)
            {
                retVal = providerFctIfPtr->loTpTransmitFct(routingPathPtr->destPduPtr->destPduHandleId, PduInfoPtr);
            }
        }
    }

    return(retVal);
}


/*******************************************************************************
* -Name            PduR_UpRxIndication
* -Brief           None
* -Details         None
* -Param[in]       RxPduId: Source Pdu Id for routing        
* -Param[in,out]   PduInfoPtr: Pdu information pointer
* -Return          None          
*******************************************************************************/
void PduR_UpRxIndication(
    PduIdType RxPduId, 
    PduInfoType* PduInfoPtr
)
{
    uint8 routingIndex;
    PduR_RoutingPathType* routingPathPtr;
    PduR_ProviderFctIfType* providerFctIfPtr;

    for(routingIndex = 0u; routingIndex < PDURCONF_ROUTINGPATH_MAX; routingIndex++)
    {
        routingPathPtr = (PduR_RoutingPathType*)&PduR_RoutingPath[routingIndex];
        if(RxPduId == routingPathPtr->srcPduPtr->sourcePduHandleId)
        {
            providerFctIfPtr = (PduR_ProviderFctIfType*)&PduR_ProviderFctIf[routingPathPtr->routingMode];
            if(NULL != providerFctIfPtr->upRxIndicationFct)
            {
                providerFctIfPtr->upRxIndicationFct(routingPathPtr->destPduPtr->destPduHandleId, PduInfoPtr);
            }
        }
    }
}


/*******************************************************************************
* -Name            PduR_UpTriggerTransmit
* -Brief           None
* -Details         None
* -Param[in]       TxPduId: Source id for routing        
* -Param[in]       info: PduInfo
* -Param[in]       bufferSizePtr:       
* -Return          E_OK: Request accomplished successful
*                  E_NOT_OK:
*******************************************************************************/
Std_ReturnType PduR_UpTriggerTransmit(
    PduIdType TxPduId, 
    PduInfoType* PduInfoPtr
)
{
    Std_ReturnType retVal = E_OK;

    uint8 routingIndex;
    PduR_RoutingPathType* routingPathPtr;
    PduR_ProviderFctIfType* providerFctIfPtr;

    for(routingIndex = 0u; routingIndex < PDURCONF_ROUTINGPATH_MAX; routingIndex++)
    {
        routingPathPtr = (PduR_RoutingPathType*)&PduR_RoutingPath[routingIndex];
        if(TxPduId == routingPathPtr->srcPduPtr->sourcePduHandleId)
        {
            providerFctIfPtr = (PduR_ProviderFctIfType*)&PduR_ProviderFctIf[routingPathPtr->routingMode];
            if(NULL != providerFctIfPtr->upTriggerTransmitFct)
            {
                retVal = providerFctIfPtr->upTriggerTransmitFct(routingPathPtr->destPduPtr->destPduHandleId, PduInfoPtr);
            }
        }
    }

    return(retVal);
}


/*******************************************************************************
* -Name            PduR_UpTxConfirmation
* -Brief           None
* -Details         None
* -Param[in]       id: Source Pdu Id for routing        
* -Param[in]       result: None
* -Return          None          
*******************************************************************************/
void PduR_UpTxConfirmation(
    PduIdType TxPduId, 
    Std_ReturnType result
)
{
    uint8 routingIndex;
    PduR_RoutingPathType* routingPathPtr;
    PduR_ProviderFctIfType* providerFctIfPtr;

    for(routingIndex = 0u; routingIndex < PDURCONF_ROUTINGPATH_MAX; routingIndex++)
    {
        routingPathPtr = (PduR_RoutingPathType*)&PduR_RoutingPath[routingIndex];
        if(TxPduId == routingPathPtr->srcPduPtr->sourcePduHandleId)
        {
            providerFctIfPtr = (PduR_ProviderFctIfType*)&PduR_ProviderFctIf[routingPathPtr->routingMode];
            if(NULL != providerFctIfPtr->upTxConfirmationFct)
            {
                providerFctIfPtr->upTxConfirmationFct(routingPathPtr->destPduPtr->destPduHandleId, result);
            }
        }
    }  
}

/*******************************************************************************
* -Name            PduR_UpTpCopyRxData
* -Brief           None
* -Details         None
* -Param[in]       id: Source id for routing        
* -Param[in]       info: PduInfo
* -Param[in]       bufferSizePtr:       
* -Return          BUFREQ_OK: Buffer request accomplished successful
*                  BUFREQ_E_NOT_OK: Buffer request not successful. Buffer cannot be accessed
*                  BUFREQ_E_BUSY: Temporarily no buffer available
*                  BUFREQ_E_OVFL: No Buffer of the required length can be provided
*******************************************************************************/
BufReq_ReturnType PduR_UpTpCopyRxData(
    PduIdType id,
    const PduInfoType* info,
    PduLengthType* bufferSizePtr
)
{
    BufReq_ReturnType retVal = BUFREQ_OK;

    uint8 routingIndex;
    PduR_RoutingPathType* routingPathPtr;
    PduR_ProviderFctIfType* providerFctIfPtr;

    for(routingIndex = 0u; routingIndex < PDURCONF_ROUTINGPATH_MAX; routingIndex++)
    {
        routingPathPtr = (PduR_RoutingPathType*)&PduR_RoutingPath[routingIndex];
        if(id == routingPathPtr->srcPduPtr->sourcePduHandleId)
        {
            providerFctIfPtr = (PduR_ProviderFctIfType*)&PduR_ProviderFctIf[routingPathPtr->routingMode];
            if(NULL != providerFctIfPtr->upTpCopyRxDataFct)
            {
                retVal = providerFctIfPtr->upTpCopyRxDataFct(routingPathPtr->destPduPtr->destPduHandleId, info, bufferSizePtr);
            }
        }
    }

    return(retVal);
}

/*******************************************************************************
* -Name            PduR_UpTpCopyTxData
* -Brief           None
* -Details         None
* -Param[in]       id: Source id for routing        
* -Param[in]       info: PduInfo
* -Param[in]       retry:
* -Param[in]       availableDataPtr:
* -Return          BUFREQ_OK: Buffer request accomplished successful
*                  BUFREQ_E_NOT_OK: Buffer request not successful. Buffer cannot be accessed
*                  BUFREQ_E_BUSY: Temporarily no buffer available
*                  BUFREQ_E_OVFL: No Buffer of the required length can be provided
*******************************************************************************/
BufReq_ReturnType PduR_UpTpCopyTxData(
    PduIdType id,
    const PduInfoType* info,
    const RetryInfoType* retry,
    PduLengthType* availableDataPtr
)
{
    BufReq_ReturnType retVal = BUFREQ_OK;
    
    uint8 routingIndex;
    PduR_RoutingPathType* routingPathPtr;
    PduR_ProviderFctIfType* providerFctIfPtr;

    for(routingIndex = 0u; routingIndex < PDURCONF_ROUTINGPATH_MAX; routingIndex++)
    {
        routingPathPtr = (PduR_RoutingPathType*)&PduR_RoutingPath[routingIndex];
        if(id == routingPathPtr->srcPduPtr->sourcePduHandleId)
        {
            providerFctIfPtr = (PduR_ProviderFctIfType*)&PduR_ProviderFctIf[routingPathPtr->routingMode];
            if(NULL != providerFctIfPtr->upTpCopyTxDataFct)
            {
                retVal = providerFctIfPtr->upTpCopyTxDataFct(routingPathPtr->destPduPtr->destPduHandleId, info, retry, availableDataPtr);
            }
        }
    }
    
    return(retVal);
}

/*******************************************************************************
* -Name            PduR_UpTpStartOfReception
* -Brief           None
* -Details         None
* -Param[in]       id: Source id for routing        
* -Param[in]       info: PduInfo
* -Param[in]       TpSduLength:
* -Param[in]       bufferSizePtr:
* -Return          BUFREQ_OK: Buffer request accomplished successful
*                  BUFREQ_E_NOT_OK: Buffer request not successful. Buffer cannot be accessed
*                  BUFREQ_E_BUSY: Temporarily no buffer available
*                  BUFREQ_E_OVFL: No Buffer of the required length can be provided
*******************************************************************************/
BufReq_ReturnType PduR_UpTpStartOfReception(
    PduIdType id,
    const PduInfoType* info,
    PduLengthType TpSduLength,
    PduLengthType* bufferSizePtr
)
{
    BufReq_ReturnType retVal = BUFREQ_OK;

    uint8 routingIndex;
    PduR_RoutingPathType* routingPathPtr;
    PduR_ProviderFctIfType* providerFctIfPtr;

    for(routingIndex = 0u; routingIndex < PDURCONF_ROUTINGPATH_MAX; routingIndex++)
    {
        routingPathPtr = (PduR_RoutingPathType*)&PduR_RoutingPath[routingIndex];
        if(id == routingPathPtr->srcPduPtr->sourcePduHandleId)
        {
            providerFctIfPtr = (PduR_ProviderFctIfType*)&PduR_ProviderFctIf[routingPathPtr->routingMode];
            if(NULL != providerFctIfPtr->upTpStartOfReceptionFct)
            {
                retVal = providerFctIfPtr->upTpStartOfReceptionFct(routingPathPtr->destPduPtr->destPduHandleId, info, TpSduLength, bufferSizePtr);
            }
        }
    }

    return(retVal);
}


/*******************************************************************************
* -Name            PduR_UpTpTpRxIndication
* -Brief           None
* -Details         None
* -Param[in]       id: Source Pdu Id for routing        
* -Param[in]       result: None
* -Return          None          
*******************************************************************************/
void PduR_UpTpTpRxIndication(
    PduIdType id,
    Std_ReturnType result
)
{
    uint8 routingIndex;
    PduR_RoutingPathType* routingPathPtr;
    PduR_ProviderFctIfType* providerFctIfPtr;

    for(routingIndex = 0u; routingIndex < PDURCONF_ROUTINGPATH_MAX; routingIndex++)
    {
        routingPathPtr = (PduR_RoutingPathType*)&PduR_RoutingPath[routingIndex];
        if(id == routingPathPtr->srcPduPtr->sourcePduHandleId)
        {
            providerFctIfPtr = (PduR_ProviderFctIfType*)&PduR_ProviderFctIf[routingPathPtr->routingMode];
            if(NULL != providerFctIfPtr->upTpTpRxIndicationFct)
            {
                providerFctIfPtr->upTpTpRxIndicationFct(routingPathPtr->destPduPtr->destPduHandleId, result);
            }
        }
    }
}


/*******************************************************************************
* -Name            PduR_UpTpTpTxConfirmation
* -Brief           None
* -Details         None
* -Param[in]       id: Source Pdu Id for routing        
* -Param[in]       result: None
* -Return          None          
*******************************************************************************/
void PduR_UpTpTpTxConfirmation(
    PduIdType id,
    Std_ReturnType result
)
{
    uint8 routingIndex;
    PduR_RoutingPathType* routingPathPtr;
    PduR_ProviderFctIfType* providerFctIfPtr;

    for(routingIndex = 0u; routingIndex < PDURCONF_ROUTINGPATH_MAX; routingIndex++)
    {
        routingPathPtr = (PduR_RoutingPathType*)&PduR_RoutingPath[routingIndex];
        if(id == routingPathPtr->srcPduPtr->sourcePduHandleId)
        {
            providerFctIfPtr = (PduR_ProviderFctIfType*)&PduR_ProviderFctIf[routingPathPtr->routingMode];
            if(NULL != providerFctIfPtr->upTpTpTxConfirmationFct)
            {
                providerFctIfPtr->upTpTpTxConfirmationFct(routingPathPtr->destPduPtr->destPduHandleId, result);
            }
        }
    }

}


